<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/11/7 11:01
 * Desc: 旅游卡
 */
class Model_Api_V2_Marketing_Strategy_Card extends Contract_Api_Marketing_Strategy
{
    private static $instance = null;

    private $application_name = '旅游卡';

    private $application_code = 'card';

    private $icon = '&#xe63b;';

    // 包含的产品id数组
    private static $product_ids = array();

    public function __construct($type_id)
    {
    }

    /**
     * @param int $type_id
     * @param array $params
     * @return Model_Api_V2_Marketing_Strategy_Card|null
     */
    public static function get_instance(int $type_id, array $params = [])
    {
        // 没有登录不检查红包
        if (!isset($params['mid'])) {
            return null;
        }

        // 应用是否安装
        if (!St_Functions::is_normal_app_install('system_card')) {
            return null;
        }

        // 有效的会员卡
        $card     = DB::select('buy_time', 'card_id', 'id', 'effective_type', 'start_date', 'end_date', 'effective_days')
            ->from('card_member')
            ->where('member_id', '=', $params['mid'])
            ->and_where('status', '>', 1)
            ->execute()
            ->as_array();
        $card_ids = array();
        foreach ($card as $value) {
            if (self::is_valid($value)) {
                $card_ids[] = $value['card_id'];
            }
        }

        if (count($card_ids)<1) {
            return null;
        }

        // 有效的产品
        $products = DB::select('type_id', 'product_id')
            ->from('card_product')
            ->where('type_id', '=', $type_id)
            ->and_where('card_id', 'IN', $card_ids)
            ->execute()
            ->as_array();
        if (empty($products)) {
            return null;
        }

        self::$product_ids = array_column($products, 'product_id');

        if (is_null(self::$instance)) {
            self::$instance = new self($type_id);
        }

        return self::$instance;
    }

    public function is_support($product_id)
    {
        $product_ids = self::$product_ids;
        if (!in_array($product_id, $product_ids)) {
            return false;
        }

        return array(
            'code' => $this->application_code,
            'name' => $this->application_name,
            'icon' => $this->icon,
        );
    }

    /**
     * 获取旅游卡有效时间
     * @param $data
     * @return string
     */
    public static function is_valid($data)
    {
        if ($data['effective_type'] == 1) {
            return true;
        } elseif ($data['effective_type'] == 2) {
            $start_time = date('Y-m-d', $data['start_date']);
            $end_time   = date('Y-m-d', $data['end_date'] . ' 23:59:59');
            return $start_time <= time() && time() <= $end_time;
        } else {
            if ($data['buy_time']) {
                $end_time = $data['buy_time'] + $data['effective_days'] * 86400;
                return $end_time > time();
            }
        }
        return false;
    }
}